import { useMemo, useCallback, useState } from 'react';
import { getBaseUrl } from '@/services/urlconst';
import { getTenantID, requestFunComponFiex } from '@/utils';
import { useRequest } from 'umi';
import { AnnounceManageAPI } from '../common/type';
import { loadMdicMap } from '@/services/BaseLayout';
import { getSearchColumns } from '../mconfig';

let baseUrl = getBaseUrl();
// let baseUrl = 'http://192.168.3.21:9001'; //小田本地
/**
 * 公告管理业务逻辑
 * @param permissions
 */
const AnnounceManageService = (permissions: Record<string, any>) => {
  /** 通过各类型获取各字典信息 */
  const loadMdicMapList = useRequest(() => {
    return loadMdicMap([
      `${getTenantID()}1201,${getTenantID()}1202,${getTenantID()}1203`,
    ]);
  });

  /**保存搜索参数 */
  const [manageTableParams, setManageTableParams] = useState<any>({});
  const tableParamsCallback = useCallback(
    (parObj: any) => {
      setManageTableParams(parObj);
    },
    [manageTableParams],
  );

  /** 各类字典树 */
  let loadMapList = useMemo(() => {
    return {
      loadMdicMapList,
      searchList: getSearchColumns(loadMdicMapList, manageTableParams),
    };
  }, [loadMdicMapList]);

  /** 表格数据请求处理业务 */
  const { tableProps, data, loading, run } = useRequest(
    (requestParams: AnnounceManageAPI.ITableRequestType) => {
      const dataGridURL = `${baseUrl}/announceController/dataGrid/${requestParams?.params}`;
      let postParams = {
        current: 1,
        pageSize: 10,
        sort: 'signTime',
        ...requestParams,
      };
      tableParamsCallback(postParams);
      return requestFunComponFiex('POST', dataGridURL, postParams);
    },
    {
      manual: true,
      paginated: true,
      defaultPageSize: 10,
    },
  );

  /** 重新请求数据 */
  const refreshRequest = useCallback(
    (requestParams: AnnounceManageAPI.ITableRequestType | any) => {
      if (requestParams?.createTime) {
        const [createTimeStart, createTimeEnd] = requestParams.createTime;
        requestParams['createTimeStart'] = createTimeStart.format(
          'YYYY-MM-DD HH:mm:ss',
        );
        requestParams['createTimeEnd'] = createTimeEnd.format(
          'YYYY-MM-DD HH:mm:ss',
        );
        delete requestParams['createTime'];
      }

      if (requestParams?.signTime) {
        const [signTimeStart, signTimeEnd] = requestParams.signTime;
        requestParams['signTimeStart'] = signTimeStart.format(
          'YYYY-MM-DD HH:mm:ss',
        );
        requestParams['signTimeEnd'] = signTimeEnd.format(
          'YYYY-MM-DD HH:mm:ss',
        );
        delete requestParams['signTime'];
      }

      if (requestParams?.cancelTime) {
        const [cancelTimeStart, cancelTimeEnd] = requestParams.cancelTime;
        requestParams['cancelTimeStart'] = cancelTimeStart.format(
          'YYYY-MM-DD HH:mm:ss',
        );
        requestParams['cancelTimeEnd'] = cancelTimeEnd.format(
          'YYYY-MM-DD HH:mm:ss',
        );
        delete requestParams['cancelTime'];
      }

      delete requestParams['signFormNameDept'];
      delete requestParams['signFormNameUser'];
      if (requestParams?.signerID && requestParams?.signerID?.length > 0) {
        requestParams['signerID'] = requestParams?.signerID?.toString();
      } else {
        delete requestParams['signerID'];
      }

      delete requestParams['cancelerFormNameDept'];
      delete requestParams['cancelerFormNameUser'];
      if (requestParams?.cancelerID && requestParams?.cancelerID?.length > 0) {
        requestParams['cancelerID'] = requestParams?.cancelerID?.toString();
      } else {
        delete requestParams['cancelerID'];
      }

      run({
        ...requestParams,
      });
    },
    [],
  );

  /** 页面数据及分页信息 */
  const announceDataInfo = useMemo(() => {
    return {
      data,
      pagination: tableProps?.pagination,
    };
  }, [data, tableProps]);

  return {
    loading,
    refreshRequest,
    manageTableParams,
    ...loadMapList,
    ...announceDataInfo,
  };
};

export { AnnounceManageService };
